'''

Case Type： 数据库系统
Case Name： 正在数据库操作时删除审计日志，不影响数据库运行
Case No. :  正在数据库操作时删除审计日志，不影响数据库运行
Create At:  2020/06/29
@zou_jialiang0505328126
Descption:  1.	进行登入登出及数据库操作2.	执行TPCC业务过程中，删除审计日志

history：
created： 2020/06/29  created by @wan005
'''
import os
import unittest
import _thread
import sys
import time
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.ComThread import *
from testcase.utils.Constant import *


class DelPg_audit(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case050.py start------------')
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.DN_NODE_NAME = macro.DN_NODE_NAME.split('/')[0]
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.PG_LOG_PATH = macro.PG_LOG_PATH
        self.PG_AUDIT_PATH = macro.PG_AUDIT_PATH + '/' + self.DN_NODE_NAME
        self.TPCC_PATH = macro.TPCC_PATH
        self.sh = CommonSH()
        self.Constant = Constant()
        self.com = Common()
        self.filename = ''

    def test_del_pgaudit(self):
        ls_cmd = 'ls -al ' + self.PG_AUDIT_PATH
        self.log.info(ls_cmd)
        ls_result = self.rootNode.sh(ls_cmd).result()
        self.log.info(ls_result)
        list_result = ls_result.splitlines()
        self.log.info(list_result)

        for i in range(50):
            sql = f'''source {self.DB_ENV_PATH}; 
                    gsql -d {self.dbUserNode.db_name} -p {self.dbUserNode.db_port} -r -l;
                '''
            # self.log.info(sql)
            msg = self.dbUserNode.sh(sql).result()
            # self.log.info(msg)

        self.filename = self.com.findlastModifiedFile(self.PG_AUDIT_PATH, '*_adt', '1', self.rootNode)
        if len(self.filename) <= 0:
            raise print('adt file did not update')

        self.log.info('------------' + self.filename + '------------')

        filesize = self.com.getfilesize(self.PG_AUDIT_PATH + '/' + self.filename, self.rootNode)

        ori_size = self.com.getOrigfilesize(list_result, self.filename)
        self.log.info('orignal size is : ' + str(ori_size))
        self.log.info('final size is : ' + str(filesize))

        self.assertTrue(ori_size < int(filesize))

        tpcc_thread = ComThread(self.com.startTPCC, args=(self.rootNode, self.TPCC_PATH,))
        tpcc_thread.start()
        time.sleep(5)

        # delete pg_audit : replace rm -rf with mv, avoid db crushing, when issue happens we can use bak file to recover
        self.log.info("delete pg_audit/file")

        deletefil_cmd = "mv " + self.PG_AUDIT_PATH + '/' + self.filename + ' ' + self.PG_AUDIT_PATH + '/' + self.filename + '.baktest'
        self.log.info(deletefil_cmd)
        self.rootNode.sh(deletefil_cmd).result()

        tpcc_thread.join(timeout=int(macro.TPCC_RUN_MAX_TIME) * 2)

        tpcc_result = tpcc_thread.get_result()
        self.log.info(tpcc_result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, tpcc_result)

    def tearDown(self):
        self.log.info('this is tearDown')
        if self.filename == '':
            pass
        else:
            self.log.info('recover pgaudit')
            recoverfil_cmd = "mv " + self.PG_AUDIT_PATH + '/' + self.filename + '.baktest' + ' ' + self.PG_AUDIT_PATH + '/' + self.filename
            self.log.info(recoverfil_cmd)
            self.rootNode.sh(recoverfil_cmd).result()
